﻿namespace zijian666.Updater.WinForm;
internal static class FormExtensions
{
    public static void Run(this Control control, Action action)
    {
        ArgumentNullException.ThrowIfNull(control);
        if (action is null)
        {
            return;
        }
        // 判断当前线程 是否是创建控件的线程
        if (control.InvokeRequired)
        {
            control.Invoke(action);
        }
        else
        {
            action();
        }
    }

    public static void Loading(this WebBrowser browser, string text = "loading...")
    {
        var html = $$"""
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>Loading</title>
  <style>
    html, body {
      height: 100%;
      margin: 0;
      padding: 0;
      overflow: hidden;
    }
    table {
      width: 100%;
      height: 100%;
      border-collapse: collapse;
    }
    td {
      text-align: center;
      vertical-align: middle;
      font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
      font-size: 20px;
      color: #555;
    }
  </style>
</head>
<body>
  <table>
    <tr>
      <td><h1>{{text}}</h1></td>
    </tr>
  </table>
</body>
</html>
""";

        browser.Run(() => browser.DocumentText = html);
    }
}
